<?php

/**
 * 获取当前毫秒数
 * @return type
 */
function getMillisecond() {
    list($t1, $t2) = explode(' ', microtime());
    return (float) sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}

/**
 * 发送验证码短信
 * @param type $telphone 手机号码
 * @return type
 */
function sendSmsCode($telphone) {
    $smsCode = generate_code();
    session(['sms_code' => $smsCode]);
    $post_data = array();
    $post_data['userid'] = 1124;
    $post_data['account'] = 'bl1505';
    $post_data['password'] = '1505aiyuan';
    $post_data['content'] = '您的验证码为' . $smsCode;
    $post_data['mobile'] = $telphone;
    $post_data['sendtime'] = ''; //不定时发送，值为0，定时发送，输入格式YYYYMMDDHHmmss的日期值
    $url = 'http://218.244.141.161:8888/sms.aspx?action=send';
    $o = '';
    foreach ($post_data as $k => $v) {
        $o .= "$k=" . urlencode($v) . '&';
    }
    $post_data = substr($o, 0, -1);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要将结果直接返回到变量里，那加上这句。
    $result = curl_exec($ch);
    return $result;
}

/**
 * 生成数字验证码
 * @param type $length
 * @return type
 */
function generate_code($length = 6) {
    return str_pad(mt_rand(0, pow(10, $length) - 1), $length, '0', STR_PAD_LEFT);
}

/**
 * 生成订单号
 * @return type
 */
function build_order_no() {
    return getMillisecond() . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
}

/**
 * 生成店铺密钥
 * @return string
 */
function generate_shop_key($id = '') {
    if (!empty($id)) {
        return md5(rand(10000000, 99999999) . 'pandacube' . $id);
    } else {
        return false;
    }
}

/**
 * 生成rfid码
 * $return 48位string的16进制
 */
function rfid_create_key() {
    
}

/**
 * 返回定时器报错页面
 * @param type $type
 * @param type $message
 * @return view
 */
function jump($message, $jumpUrl = '/admin/commodity', $type = 'error') {
    return view('jump', ['type' => $type, 'jumpUrl' => $jumpUrl, 'message' => $message]);
}

/**
 * 去除多维数组中的空值
 * @author 
 * @return mixed
 * @param $arr 目标数组
 * @param array $values 去除的值  默认 去除  '',null,false,0,'0',[]
 */
function filter_array($arr, $values = ['', null, false, 0, '0', []]) {
    foreach ($arr as $k => $v) {
        if (is_array($v) && count($v) > 0) {
            $arr[$k] = filter_array($v, $values);
        }
        foreach ($values as $value) {
            if ($v === $value) {
                unset($arr[$k]);
                break;
            }
        }
    }
    return $arr;
}

function getTree($items,$pid ="fid") {
    $map  = [];
    $tree = [];    
    foreach ($items as &$it){ $map[$it['id']] = &$it; }  //数据的ID名生成新的引用索引树
    foreach ($items as &$it){
        $parent = &$map[$it[$pid]];
        if($parent) {
            $parent['childrens'][] = &$it;
        }else{
            $tree[] = &$it;
        }
    }
    return $tree;
}
